package com.yanqu.road.server.manger;

import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.entity.servercenter.ChannelSystemConfig;
import com.yanqu.road.entity.servercenter.SystemConfigChannel;
import com.yanqu.road.logic.redis.RedisKeyDefine;
import com.yanqu.road.logic.redis.RedisKeyDefineHelper;
import com.yanqu.road.logic.redis.RedisManger;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.plugin.ManagerReloadListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChannelSystemConfigMgr extends TempMgr {
    //key:channelId
    private static Map<Integer, SystemConfigChannel> channelSystemConfigMap = new HashMap<>();
    private static List<ManagerReloadListener> managerReloadListener = new ArrayList<>();

    @Override
    public boolean init() throws Exception {
        return reloadConfig() && reloadData();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static Map<String, ChannelSystemConfig> getSystemConfigMap(int channelId, int systemId) {
        SystemConfigChannel systemConfigChannel = channelSystemConfigMap.get(channelId);
        if (systemConfigChannel == null) {
            return new HashMap<>();
        }
        return systemConfigChannel.getSystemConfigMap(systemId);
    }

    protected static SystemConfigChannel setConfigMap(int channelId, Map<Integer, Map<String, ChannelSystemConfig>> configMap) {
        SystemConfigChannel systemConfigChannel = channelSystemConfigMap.get(channelId);
        if (systemConfigChannel == null) {
            synchronized (channelSystemConfigMap){
                systemConfigChannel = channelSystemConfigMap.get(channelId);
                if (systemConfigChannel == null) {
                    systemConfigChannel = new SystemConfigChannel(channelId);
                    channelSystemConfigMap.put(channelId, systemConfigChannel);
                }
            }
        }
        systemConfigChannel.setChannelSystemConfigMap(configMap);
        return systemConfigChannel;
    }

    public static void reloadDataFromRedis(int channelId) {
        getLogger().info("reloadDataFromRedis start. channelId:{}", channelId);
        Map<Integer, Map<String, ChannelSystemConfig>> systemConfigMap = RedisManger.getRedisUtil().getObject(RedisKeyDefineHelper.getChannelSystemConfigKey(channelId), new TypeReference<Map<Integer, Map<String, ChannelSystemConfig>>>() {
        });
        if (systemConfigMap == null) {
            return;
        }
        setConfigMap(channelId, systemConfigMap);
        notifyReload();

        getLogger().info("reloadDataFromRedis end. channelId:{}", channelId);
    }

    protected static void notifyReload(){
        for (ManagerReloadListener listener : managerReloadListener) {
            listener.notifyReload();
        }
    }

    public static void addReloadListener(ManagerReloadListener reloadListener){
        managerReloadListener.add(reloadListener);
    }
}
